package com.example.mycommunity.controller;

import com.example.mycommunity.dto.PageDTO;
import com.example.mycommunity.model.User;
import com.example.mycommunity.service.NotificationService;
import com.example.mycommunity.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class ProfileController {


    @Autowired
    private QuestionService questionService;

    @Autowired
    private NotificationService notificationService;

    @GetMapping("/profile/{action}")
    public String profile(@PathVariable(name = "action")String action,
                          Model model,
                          HttpServletRequest request,
                          @RequestParam(name = "page",defaultValue = "1")Integer page,
                          @RequestParam(name = "size",defaultValue = "10")Integer size){
        User user = (User) request.getSession().getAttribute("user");

        if(user == null){
            return "redirect:/";
        }

        if("questions".equals(action)){
            model.addAttribute("section","questions");
            model.addAttribute("sectionName","我的提问");

            PageDTO pageDTO = questionService.list(user.getId(), page, size);
            model.addAttribute("pageDTO",pageDTO);
        }else if("replies".equals(action)){

            PageDTO pageDTO = notificationService.list(user.getId(),page,size);

            model.addAttribute("pageDTO",pageDTO);
            model.addAttribute("section","replies");
            model.addAttribute("sectionName","最新回复");

        }



        return "profile";
    }

}
